/*
 * @Author: early-autumn
 * @Date: 2020-05-12 20:51:39
 * @LastEditors: early-autumn
 * @LastEditTime: 2020-05-21 11:20:30
 */
import { getDicts } from '@/api/system/dict/data'
import store from '../index'

function effect(key, value) {
  if (value.length === 0) {
    store.dispatch('dict/send', key)
  }

  return value
}

function getKey(key) {
  return `dict::${key}`
}

async function setDict(key) {
  const { data } = await getDicts(key)

  const dict = JSON.stringify({
    updateTime: new Date(),
    value: data || [],
  })

  Promise.resolve().then(() => localStorage.setItem(getKey(key), dict))

  return data
}

async function getDict(key) {
  const json = JSON.parse(localStorage.getItem(getKey(key)))

  if (json) {
    const { updateTime, value } = json

    const time = new Date(updateTime).toLocaleDateString()
    const currentTime = new Date().toLocaleDateString()

    if (value && value.length !== 0 && time === currentTime) {
      return value
    }
  }

  return setDict(key)
}

export default {
  namespaced: true,
  state: {
    sys_user_sex: [],
    sys_customer_status: [],
    sys_phone_ownership: [],
    sys_tracking_status: [],
    sys_consumption_status: [],
    sys_billing_status: [],
    sys_sweep_code_status: [],
    sys_doctor_followed_status: [],
    sys_reach_state: [],
    sys_tc_return_status: [],
    sys_seeing_status: [],
    sys_clinch_deal_status: [],
    sys_markt_profession: [],
    sys_have_child: [],
    sys_markt_marry: [],
    sys_markt_character: [],
    sys_markt_habit: [],
    sys_markt_ability: [],
    sys_referrer_relationship: [],
    sys_doctor_rest_status: [],
    sys_markt_staus: [],
    sys_make_project: [],
    sys_markt_entity: [],
    sys_yes_no: [],
    sys_return_status: [],
    sys_disease: [],
    sys_attendance_status: [],
    sys_case_clinic_status: [],
    sys_case_type: [],
    sys_resource_shadow: [],
    sys_channel_status: [],
    vipLevel_list: [],
    sys_payment: [],
  },
  mutations: {
    save(state, { key, value }) {
      state[key] = value
    },
  },
  actions: {
    async send({ commit }, key) {
      commit('save', { key, value: await getDict(key) })
    },
  },
  getters: {
    paymentMode({ sys_payment }) {
      return effect('sys_payment', sys_payment)
    },
    vipLevel({ vipLevel_list }) {
      return effect('vipLevel_list', vipLevel_list)
    },
    channelStatus({ sys_channel_status }) {
      return effect('sys_channel_status', sys_channel_status)
    },
    resourceShadow({ sys_resource_shadow }) {
      return effect('sys_resource_shadow', sys_resource_shadow)
    },
    //病历类型
    caseType({ sys_case_type }) {
      return effect('sys_case_type', sys_case_type)
    },
    //病历就诊状态
    caseAttendanceStatus({ sys_case_clinic_status }) {
      return effect('sys_case_clinic_status', sys_case_clinic_status)
    },
    //就诊状态
    attendanceStatus({ sys_attendance_status }) {
      return effect('sys_attendance_status', sys_attendance_status)
    },
    //回访状态
    callBackStateOptions({ sys_return_status }) {
      return effect('sys_return_status', sys_return_status)
    },
    //是否大单
    yesOrNo({ sys_yes_no }) {
      return effect('sys_yes_no', sys_yes_no)
    },
    //预约病种
    marketEntityOptions({ sys_markt_entity }) {
      return effect('sys_markt_entity', sys_markt_entity)
    },
    //预约项目
    marketMakeProject({ sys_make_project }) {
      return effect('sys_make_project', sys_make_project)
    },
    // 状态
    marketStatusOptions({ sys_markt_staus }) {
      return effect('sys_markt_staus', sys_markt_staus)
    },
    // 性别
    sexOptions({ sys_user_sex }) {
      return effect('sys_user_sex', sys_user_sex)
    },
    // 客户状态
    customerStatusOptions({ sys_customer_status }) {
      return effect('sys_customer_status', sys_customer_status)
    },
    // 电话所属关系
    phoneOwnershipOptions({ sys_phone_ownership }) {
      return effect('sys_phone_ownership', sys_phone_ownership)
    },
    // 跟踪状态
    trackingStatusOptions({ sys_tracking_status }) {
      return effect('sys_tracking_status', sys_tracking_status)
    },
    // 消费状态
    consumptionStatusOptions({ sys_consumption_status }) {
      return effect('sys_consumption_status', sys_consumption_status)
    },
    // 开单状态
    billingStatusOptions({ sys_billing_status }) {
      return effect('sys_billing_status', sys_billing_status)
    },
    // 扫码状态
    sweepCodeStatusOptions({ sys_sweep_code_status }) {
      return effect('sys_sweep_code_status', sys_sweep_code_status)
    },
    // 医生随访状态
    doctorFollowedStatusOptions({ sys_doctor_followed_status }) {
      return effect('sys_doctor_followed_status', sys_doctor_followed_status)
    },
    // 到店状态
    reachStatusOptions({ sys_reach_state }) {
      return effect('sys_reach_state', sys_reach_state)
    },
    // TC回访状态
    tcReturnStatusOptions({ sys_tc_return_status }) {
      return effect('sys_tc_return_status', sys_tc_return_status)
    },
    // 看诊状态
    seeingStatusOptions({ sys_seeing_status }) {
      return effect('sys_seeing_status', sys_seeing_status)
    },
    // 成交状态
    clinchDealStatusOptions({ sys_clinch_deal_status }) {
      return effect('sys_clinch_deal_status', sys_clinch_deal_status)
    },
    // 职业
    professionalOptions({ sys_markt_profession }) {
      return effect('sys_markt_profession', sys_markt_profession)
    },
    // 有无小孩
    haveChildOptions({ sys_have_child }) {
      return effect('sys_have_child', sys_have_child)
    },
    // 婚姻状况
    maritalStatusOptions({ sys_markt_marry }) {
      return effect('sys_markt_marry', sys_markt_marry)
    },
    // 性格
    characterOptions({ sys_markt_character }) {
      return effect('sys_markt_character', sys_markt_character)
    },
    // 消费习惯
    spendingHabitsOptions({ sys_markt_habit }) {
      return effect('sys_markt_habit', sys_markt_habit)
    },
    // 经济能力
    economicAbilityOptions({ sys_markt_ability }) {
      return effect('sys_markt_ability', sys_markt_ability)
    },
    // 推荐人关系
    referrerRelationOptions({ sys_referrer_relationship }) {
      return effect('sys_referrer_relationship', sys_referrer_relationship)
    },
    // 医生休息状态
    doctorRestStatusOptions({ sys_doctor_rest_status }) {
      return effect('sys_doctor_rest_status', sys_doctor_rest_status)
    },
    // 病种
    diseaseOptions({ sys_disease }) {
      return effect('sys_disease', sys_disease)
    },
  },
}
